﻿using System.ComponentModel;
using System.Reflection;

namespace FlyingHat.Common.Helper
{
    public static class EnumHelper
    {
        /// <summary>
        /// 获取枚举值的Description信息
        /// </summary>
        /// <param name ="value">枚举值</param>
        /// <param name ="args">要格式化的对象</param>
        /// <returns>如果未找到DescriptionAttribute则返回null或返回类型描述</returns>
        public static string GetDescription(this Enum value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            FieldInfo? fi = value.GetType().GetField(value.ToString());
            if (fi != null)
            {
                var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                var text = attributes.Length > 0 ? attributes[0].Description : value.ToString();
                return text;
            }
            return string.Empty;
        }

        public static List<EnumItem> EnumItems(Type enumType)
        {
            List<EnumItem> listItem = new List<EnumItem>();
            if (enumType != null && enumType.IsEnum)
            {
                Array values = Enum.GetValues(enumType);
                if (values.Length > 0)
                {
                    foreach (int item in values)
                    {
                        var text = Enum.GetName(enumType, item);
                        if (string.IsNullOrWhiteSpace(text))
                        {
                            continue;
                        }
                        FieldInfo? fi = enumType.GetField(text);
                        if (fi == null)
                        {
                            continue;
                        }
                        var attr = Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                        if (attr != null)
                        {
                            var des = attr as DescriptionAttribute;
                            var en = new EnumItem()
                            {
                                Value = item,
                                Text = text
                            };
                            if (des != null && !string.IsNullOrWhiteSpace(des.Description))
                            {
                                en.Description1 = des.Description;
                            }
                            listItem.Add(en);
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentException("请传入正确的枚举！");
            }
            return listItem;
        }
    }

    public class EnumItem
    {
        /// <summary>
        /// 枚举值
        /// </summary>
        public int Value { get; set; }
        /// <summary>
        /// 枚举文本
        /// </summary>
        public string Text { get; set; } = string.Empty;
        /// <summary>
        /// 描述1
        /// </summary>
        public string Description1 { get; set; } = string.Empty;
        /// <summary>
        /// 描述2
        /// </summary>
        public string Description2 { get; set; } = string.Empty;
    }
}
